package project3t.android.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import android.R.string;

import project3t.android.control.Controller;
import project3t.android.data.Person;

public class RestConnect {

	private static String url="http://192.168.0.125:8080/TestServer/PeopleServlet";
	public static List makeRequest()
	{
		// The URL for making the GET request		
		// Set the Accept header for "application/json"
		List<MediaType> acceptableMediaTypes=new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
		HttpHeaders requestHeaders=new HttpHeaders();
		requestHeaders.setAccept(acceptableMediaTypes);
		// Populate the headers in an HttpEntity object to use for the request
		HttpEntity<?> requestEntity=new HttpEntity<Object>(requestHeaders);				
		// Create a new RestTemplate instance
		RestTemplate restTemplate=new RestTemplate();
		// Perform the HTTP GET request
		ResponseEntity<Person[]> responseEntity=restTemplate.exchange(url, HttpMethod.GET, requestEntity, Person[].class);
		// convert the array to a list and return it
		return Arrays.asList(responseEntity.getBody());		
	}
	public static void sendData()
	{
		Person person=Controller.conroller.getPersonByPos(0);
		
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setContentType(new MediaType("application","json"));
		HttpEntity<Person> requestEntity = new HttpEntity<Person>(person, requestHeaders);
		
		
		
		RestTemplate restTemplate=new RestTemplate();
		ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
		String result = responseEntity.getBody();
	}
}
